home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Muzyka / Edytory sampli (probek dzwieku) / ZynAddSubFX_2.2.0 / Setup_ZynAddSubFX-2.2.0.exe / source code / Params / Controller.C < prev    next >
C/C++ Source or Header  |  2005-03-14  |  10KB  |  301 lines

  1. /*
  2.   ZynAddSubFX - a software synthesizer
  3.  
  4.   Controller.C - (Midi) Controllers implementation
  5.   Copyright (C) 2002-2005 Nasca Octavian Paul
  6.   Author: Nasca Octavian Paul
  7.  
  8.   This program is free software; you can redistribute it and/or modify
  9.   it under the terms of version 2 of the GNU General Public License 
  10.   as published by the Free Software Foundation.
  11.  
  12.   This program is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License (version 2) for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License (version 2)
  18.   along with this program; if not, write to the Free Software Foundation,
  19.   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20.  
  21. */
  22.  
  23. #include "Controller.h"
  24. #include <math.h>
  25. #include <stdio.h>
  26.  
  27. Controller::Controller(){
  28.     defaults();
  29.     resetall();
  30. };
  31.  
  32. Controller::~Controller(){
  33. };
  34.  
  35. void Controller::defaults(){
  36.     setpitchwheelbendrange(200);//2 halftones
  37.     expression.receive=1;
  38.     panning.depth=64;
  39.     filtercutoff.depth=64;
  40.     filterq.depth=64;
  41.     bandwidth.depth=64;
  42.     bandwidth.exponential=0;
  43.     modwheel.depth=80;
  44.     modwheel.exponential=0;
  45.     fmamp.receive=1;
  46.     volume.receive=0;
  47.     sustain.receive=1;
  48.     NRPN.receive=1;
  49.  
  50.     portamento.portamento=0;
  51.     portamento.used=0;
  52.     portamento.receive=1;
  53.     portamento.time=64;
  54.     portamento.updowntimestretch=64;
  55.     portamento.pitchthresh=3;
  56.     portamento.pitchthreshtype=1;
  57.     portamento.noteusing=-1;
  58.     resonancecenter.depth=64;
  59.     resonancebandwidth.depth=64;
  60.  
  61.     initportamento(440.0,440.0);
  62.     setportamento(0);
  63.  
  64. };
  65.  
  66. void Controller::resetall(){
  67.     setpitchwheel(0);//center
  68.     setexpression(127);
  69.     setpanning(64);
  70.     setfiltercutoff(64);
  71.     setfilterq(64);
  72.     setbandwidth(64);
  73.     setmodwheel(64);
  74.     setfmamp(127);
  75.     setvolume(127);
  76.     setsustain(0);
  77.     setresonancecenter(64);
  78.     setresonancebw(64);
  79.     
  80.     //reset the NRPN
  81.     NRPN.parhi=-1;
  82.     NRPN.parlo=-1;
  83.     NRPN.valhi=-1;
  84.     NRPN.vallo=-1;
  85. };
  86.  
  87. void Controller::setpitchwheel(int value){
  88.     pitchwheel.data=value;
  89.     REALTYPE cents=value/8192.0;
  90.     cents*=pitchwheel.bendrange;
  91.     pitchwheel.relfreq=pow(2,cents/1200.0);
  92.     //fprintf(stderr,"%ld %ld -> %.3f\n",pitchwheel.bendrange,pitchwheel.data,pitchwheel.relfreq);fflush(stderr);
  93. };
  94.  
  95. void Controller::setpitchwheelbendrange(unsigned short int value){
  96.     pitchwheel.bendrange=value;
  97. };
  98.  
  99. void Controller::setexpression(int value){
  100.     expression.data=value;
  101.     if (expression.receive!=0) expression.relvolume=value/127.0;
  102.     else expression.relvolume=1.0;
  103. };
  104.  
  105. void Controller::setpanning(int value){
  106.     panning.data=value;
  107.     panning.pan=(value/128.0-0.5)*(panning.depth/64.0);
  108. };
  109.  
  110. void Controller::setfiltercutoff(int value){
  111.     filtercutoff.data=value;
  112.     filtercutoff.relfreq=(value-64.0)*filtercutoff.depth/4096.0*3.321928;//3.3219..=ln2(10)
  113. };
  114.  
  115. void Controller::setfilterq(int value){
  116.     filterq.data=value;
  117.     filterq.relq=pow(30.0,(value-64.0)/64.0*(filterq.depth/64.0));
  118. };
  119.  
  120. void Controller::setbandwidth(int value){
  121.     bandwidth.data=value;
  122.     if (bandwidth.exponential==0) {
  123.         REALTYPE tmp=pow(25.0,pow(bandwidth.depth/127.0,1.5))-1.0;
  124.         if ((value<64)&&(bandwidth.depth>=64)) tmp=1.0;
  125.         bandwidth.relbw=(value/64.0-1.0)*tmp+1.0;
  126.         if (bandwidth.relbw<0.01) bandwidth.relbw=0.01;
  127.     } else {
  128.     bandwidth.relbw=pow(25.0,(value-64.0)/64.0*(bandwidth.depth/64.0));
  129.     };
  130. };
  131.  
  132. void Controller::setmodwheel(int value){
  133.     modwheel.data=value;
  134.     if (modwheel.exponential==0) {
  135.         REALTYPE tmp=pow(25.0,pow(modwheel.depth/127.0,1.5)*2.0)/25.0;
  136.         if ((value<64)&&(modwheel.depth>=64)) tmp=1.0;
  137.         modwheel.relmod=(value/64.0-1.0)*tmp+1.0;
  138.         if (modwheel.relmod<0.0) modwheel.relmod=0.0;
  139.     } else modwheel.relmod=pow(25.0,(value-64.0)/64.0*(modwheel.depth/80.0));
  140. };
  141.  
  142. void Controller::setfmamp(int value){
  143.     fmamp.data=value;
  144.     fmamp.relamp=value/127.0;
  145.     if (fmamp.receive!=0) fmamp.relamp=value/127.0;
  146.     else fmamp.relamp=1.0;
  147. };
  148.  
  149. void Controller::setvolume(int value){
  150.     volume.data=value;
  151.     if (volume.receive!=0) volume.volume=pow(0.1,(127-value)/127.0*2.0);
  152.     else volume.volume=1.0;
  153. };
  154.  
  155. void Controller::setsustain(int value){
  156.     sustain.data=value;
  157.     if (sustain.receive!=0) sustain.sustain=((value<64) ? 0 : 1 );
  158.     else sustain.sustain=0;
  159. };
  160.  
  161. void Controller::setportamento(int value){
  162.     portamento.data=value;
  163.     if (portamento.receive!=0) portamento.portamento=((value<64) ? 0 : 1 );
  164. };
  165.  
  166. int Controller::initportamento(REALTYPE oldfreq,REALTYPE newfreq){
  167.     portamento.x=0.0;
  168.     if ((portamento.used!=0) || (portamento.portamento==0)) return(0);
  169.     REALTYPE portamentotime=pow(100.0,portamento.time/127.0)/50.0;//portamento time in seconds
  170.  
  171.     if ((portamento.updowntimestretch>=64)&&(newfreq<oldfreq)){
  172.     if (portamento.updowntimestretch==127) return(0);
  173.     portamentotime*=pow(0.1,(portamento.updowntimestretch-64)/63.0);
  174.     } 
  175.     if ((portamento.updowntimestretch<64)&&(newfreq>oldfreq)){
  176.     if (portamento.updowntimestretch==0) return(0);
  177.     portamentotime*=pow(0.1,(64.0-portamento.updowntimestretch)/64.0);
  178.     };
  179.     
  180.     portamento.dx=SOUND_BUFFER_SIZE/(portamentotime*SAMPLE_RATE);
  181.     portamento.origfreqrap=oldfreq/newfreq;
  182.     
  183.     REALTYPE tmprap=( (portamento.origfreqrap>1.0) ? 
  184.                       (portamento.origfreqrap) : 
  185.               (1.0/portamento.origfreqrap) );
  186.     
  187.     REALTYPE thresholdrap=pow(2.0,portamento.pitchthresh/12.0);
  188.     if ((portamento.pitchthreshtype==0) && (tmprap-0.00001>thresholdrap) ) return(0);
  189.     if ((portamento.pitchthreshtype==1) && (tmprap+0.00001<thresholdrap) ) return(0);
  190.  
  191.     portamento.used=1;
  192.     portamento.freqrap=portamento.origfreqrap;
  193.     return (1);
  194. };
  195.  
  196. void Controller::updateportamento(){
  197.     if (portamento.used==0) return;
  198.     
  199.     portamento.x+=portamento.dx;
  200.     if (portamento.x>1.0) {
  201.     portamento.x=1.0;
  202.     portamento.used=0;
  203.     };
  204.     portamento.freqrap=(1.0-portamento.x)*portamento.origfreqrap+portamento.x;
  205. };
  206.  
  207.  
  208. void Controller::setresonancecenter(int value){
  209.     resonancecenter.data=value;
  210.     resonancecenter.relcenter=pow(3.0,(value-64.0)/64.0*(resonancecenter.depth/64.0));
  211. };
  212. void Controller::setresonancebw(int value){
  213.     resonancebandwidth.data=value;
  214.     resonancebandwidth.relbw=pow(1.5,(value-64.0)/64.0*(resonancebandwidth.depth/127.0));
  215. };
  216.  
  217.  
  218. //Returns 0 if there is NRPN or 1 if there is not
  219. int Controller::getnrpn(int *parhi, int *parlo, int *valhi, int *vallo){
  220.     if (NRPN.receive==0) return(1);
  221.     if ((NRPN.parhi<0)||(NRPN.parlo<0)||(NRPN.valhi<0)||(NRPN.vallo<0)) 
  222.     return(1);
  223.     
  224.     *parhi=NRPN.parhi;
  225.     *parlo=NRPN.parlo;
  226.     *valhi=NRPN.valhi;
  227.     *vallo=NRPN.vallo;
  228.     return(0);
  229. };
  230.  
  231.  
  232. void Controller::setparameternumber(unsigned int type,int value){
  233.     switch(type){
  234.     case C_nrpnhi:NRPN.parhi=value;
  235.               NRPN.valhi=-1;NRPN.vallo=-1;//clear the values
  236.               break;
  237.     case C_nrpnlo:NRPN.parlo=value;
  238.               NRPN.valhi=-1;NRPN.vallo=-1;//clear the values
  239.               break;
  240.     case C_dataentryhi:if ((NRPN.parhi>=0)&&(NRPN.parlo>=0)) NRPN.valhi=value;
  241.                break;
  242.     case C_dataentrylo:if ((NRPN.parhi>=0)&&(NRPN.parlo>=0)) NRPN.vallo=value;
  243.                break;
  244.     };
  245. };
  246.  
  247.  
  248.  
  249. void Controller::add2XML(XMLwrapper *xml){
  250.     xml->addpar("pitchwheel_bendrange",pitchwheel.bendrange);
  251.  
  252.     xml->addparbool("expression_receive",expression.receive);
  253.     xml->addpar("panning_depth",panning.depth);
  254.     xml->addpar("filter_cutoff_depth",filtercutoff.depth);
  255.     xml->addpar("filter_q_depth",filterq.depth);
  256.     xml->addpar("bandwidth_depth",bandwidth.depth);
  257.     xml->addpar("mod_wheel_depth",modwheel.depth);
  258.     xml->addparbool("mod_wheel_exponential",modwheel.exponential);
  259.     xml->addparbool("fm_amp_receive",fmamp.receive);
  260.     xml->addparbool("volume_receive",volume.receive);
  261.     xml->addparbool("sustain_receive",sustain.receive);
  262.  
  263.     xml->addparbool("portamento_receive",portamento.receive);
  264.     xml->addpar("portamento_time",portamento.time);
  265.     xml->addpar("portamento_pitchthresh",portamento.pitchthresh);
  266.     xml->addpar("portamento_pitchthreshtype",portamento.pitchthreshtype);
  267.     xml->addpar("portamento_portamento",portamento.portamento);
  268.     xml->addpar("portamento_updowntimestretch",portamento.updowntimestretch);
  269.  
  270.     xml->addpar("resonance_center_depth",resonancecenter.depth);
  271.     xml->addpar("resonance_bandwidth_depth",resonancebandwidth.depth);
  272. };
  273.  
  274. void Controller::getfromXML(XMLwrapper *xml){
  275.     pitchwheel.bendrange=xml->getpar("pitchwheel_bendrange",pitchwheel.bendrange,-6400,6400);
  276.  
  277.     expression.receive=xml->getparbool("expression_receive",expression.receive);
  278.     panning.depth=xml->getpar127("panning_depth",panning.depth);
  279.     filtercutoff.depth=xml->getpar127("filter_cutoff_depth",filtercutoff.depth);
  280.     filterq.depth=xml->getpar127("filter_q_depth",filterq.depth);
  281.     bandwidth.depth=xml->getpar127("bandwidth_depth",bandwidth.depth);
  282.     modwheel.depth=xml->getpar127("mod_wheel_depth",modwheel.depth);
  283.     modwheel.exponential=xml->getparbool("mod_wheel_exponential",modwheel.exponential);
  284.     fmamp.receive=xml->getparbool("fm_amp_receive",fmamp.receive);
  285.     volume.receive=xml->getparbool("volume_receive",volume.receive);
  286.     sustain.receive=xml->getparbool("sustain_receive",sustain.receive);
  287.  
  288.     portamento.receive=xml->getparbool("portamento_receive",portamento.receive);
  289.     portamento.time=xml->getpar127("portamento_time",portamento.time);
  290.     portamento.pitchthresh=xml->getpar127("portamento_pitchthresh",portamento.pitchthresh);
  291.     portamento.pitchthreshtype=xml->getpar127("portamento_pitchthreshtype",portamento.pitchthreshtype);
  292.     portamento.portamento=xml->getpar127("portamento_portamento",portamento.portamento);
  293.     portamento.updowntimestretch=xml->getpar127("portamento_updowntimestretch",portamento.updowntimestretch);
  294.  
  295.     resonancecenter.depth=xml->getpar127("resonance_center_depth",resonancecenter.depth);
  296.     resonancebandwidth.depth=xml->getpar127("resonance_bandwidth_depth",resonancebandwidth.depth);
  297. };
  298.  
  299.  
  300.  
  301.